package com.anmingle.pswb.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.anmingle.common.R;
import com.anmingle.common.ResponseResult;
import com.anmingle.enums.ErrorCode;
import com.anmingle.exception.BusinessException;
import com.anmingle.pswb.entity.LoginUser;
import com.anmingle.pswb.model.dto.UserRegisterRequest;
import com.anmingle.pswb.model.dto.WxLoginRequest;
import com.anmingle.pswb.model.entity.User;
import com.anmingle.pswb.service.UserService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static cn.dev33.satoken.stp.StpUtil.checkLogin;


/**
 * @author 安明乐
 * @Title UserController
 * @Package IntelliJ IDEA
 * @date 2022/11/18 17:01
 */
@RestController
@RequestMapping("/ps_user")
public class UserController {



    @Resource
    private UserService userService;

    private record LoginRequest(
            String user,
            String password
    ) { }


    @DeleteMapping("/de")
    public ResponseResult<Boolean> deleteUser(Long id) {
        if (StringUtils.isAnyBlank(String.valueOf(id))) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Object loginId = StpUtil.getLoginId();

        boolean b = userService.removeById(id);
        StpUtil.logout(id);

        return R.ok(b);
    }


    @PostMapping("/register")
    public ResponseResult<Long> userRegister(@RequestBody UserRegisterRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String user = request.getUser();
        String password = request.getPassword();
        String phone = request.getPhone();
        String name = request.getName();
        String checkPassword = request.getCheckPassword();

        if (StringUtils.isAnyBlank(user, password, checkPassword, phone)) {
            throw new BusinessException(401,"参数不能为空");
        }
        long result = userService.userRegister(user, password
                , checkPassword, phone, name);

        return R.ok(result);
    }


    @PostMapping("/out")
    public ResponseResult<Boolean> loginOut() {
        boolean b = userService.loginOut();
        return R.ok(b);
    }

    @GetMapping("/checkLogin")
    public Boolean checkLogin() {
        return  StpUtil.isLogin();
    }


    @PostMapping("/login")
    public ResponseResult<Map<String, Object>> login(@RequestBody LoginRequest user) {
        if (StpUtil.isLogin()) {
            throw new RuntimeException("您已登录");
        }
        if (StringUtils.isAnyBlank(user.user(), user.password())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Map<String, Object> login = userService.login(user.user(), user.password());
        return R.ok("登录成功", login);
    }


    @PostMapping("/WXLogin")
    public ResponseResult<Map<String, Object>> WxLogin(@RequestBody WxLoginRequest request) {
        if (StpUtil.isLogin()) {
            throw new RuntimeException("您已登录");
        }
        String nickname = request.getNickname();
        String image = request.getImage();
        String password = request.getPassword();
        String checkPassword = request.getCheckPassword();
        String phone = request.getPhone();
        String openid = request.getOpenid();

        if (StringUtils.isAnyBlank(openid, nickname, image, password, checkPassword, phone)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Map<String, Object> login = userService.WXlogin(openid, nickname, image, password, checkPassword, phone);


        return R.ok("登录成功", login);
    }

    @GetMapping("/getUser")
    public ResponseResult<LoginUser> getUser() {

        LoginUser user = userService.getUser();

        return R.ok(user);
    }



}
